  PROGRAM TSTORMS_DRIVE
!===================================================================

   use TSTORMS_MOD, only : TSTORMS, TSTORMS_NOSPLINE, TSTORMS_HODGES, TSTORMS_WINTER, &
                           SET_TSTORMS
   use INDATA_MOD,  only : SET_GRID, SET_LOLA, GET_DATA, GET_DATA_HODGES, &
                           imx, jmx, nmx
   implicit none

!-------------------------------------------------------------------

  integer, parameter :: iucy = 12

  real, allocatable, dimension(:)   :: rlon, rlat
  real, allocatable, dimension(:,:) :: wind, vor, tbar, psl, thick, &
                                       vor200, vor500, vor850_t42

  integer :: year, month, day, hour
  integer :: n
  real    :: rmax, rmin

!=====================================================================
! --- NAMELIST
!=====================================================================

  character*16 :: tracktype = 'gfdl'

  namelist / nml_tstormsdrive / tracktype

!-------------------------------------------------------------------
! --- INITALIZE
!-------------------------------------------------------------------

  OPEN( iucy, FILE = 'cyclones' )
  
  READ( *, nml_tstormsdrive )
  WRITE( *, nml_tstormsdrive )
  
  CALL SET_TSTORMS
 
  !PRINT *, 'Checkpoint 1'

  CALL SET_GRID

  ALLOCATE(  rlon(imx) )
  ALLOCATE(  rlat(jmx) )
  ALLOCATE(  wind(imx,jmx) )
  ALLOCATE(   vor(imx,jmx) )
  ALLOCATE(   psl(imx,jmx) )
  if (tracktype .eq. 'gfdl' .OR. tracktype .eq. 'colin' .OR. tracktype .eq. 'winter') then
    ALLOCATE(  tbar(imx,jmx) )
    ALLOCATE( thick(imx,jmx) )
  elseif (tracktype .eq. 'hodges') then              
    ALLOCATE( vor200(imx,jmx) )
    ALLOCATE( vor500(imx,jmx) )
    ALLOCATE( vor850_t42(imx,jmx) )
  else
    print *,'Unknown tracker selection... exiting...'
    CALL EXIT(69) 
  endif

  CALL SET_LOLA( rlon, rlat )

!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  do n = 1,nmx                               ! --- TIME LOOP STARTS
!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

!-------------------------------------------------------------------
! --- INPUT DATA
!-------------------------------------------------------------------
                 
  if (tracktype .eq. 'gfdl' .OR. tracktype .eq. 'colin' .OR. tracktype .eq. 'winter') then
     CALL GET_DATA( n, wind, vor, tbar, psl, thick, &
                 year, month, day, hour)
   elseif (tracktype .eq. 'hodges') then              
     CALL GET_DATA_HODGES( n, wind, vor, vor850_t42, vor200, vor500, psl, &
                 year, month, day, hour)
   else
     print *,'Unknown tracker selection... exiting...'
   endif
   
  print *, '   '
  print *, ' year, month, day, hour = ', year, month, day, hour
  print *, '   '

!-------------------------------------------------------------------
! --- FIND STORMS
!-------------------------------------------------------------------

   if (tracktype .eq. 'gfdl') then
     CALL TSTORMS ( wind, vor,  tbar, psl,   thick,    &
                  rlon, rlat, year, month, day, hour,  &
                  iucy )
   elseif (tracktype .eq. 'colin') then
     CALL TSTORMS_NOSPLINE ( wind, vor,  tbar, psl,   thick,    &
                  rlon, rlat, year, month, day, hour,  &
                  iucy )
   elseif (tracktype .eq. 'winter') then
     CALL TSTORMS_WINTER ( wind, vor, psl,    &
                  rlon, rlat, year, month, day, hour,  &
                  iucy )
   elseif (tracktype .eq. 'hodges') then              
     CALL TSTORMS_HODGES ( wind, vor,  vor850_t42, vor200, vor500, &
                  psl,    &
                  rlon, rlat, year, month, day, hour,  &
                  iucy )
   else
     print *,'Unknown tracker selection... exiting...'
   endif

!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  end do                                       ! --- TIME LOOP ENDS
!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

!-------------------------------------------------------------------
! --- CLEAN UP
!-------------------------------------------------------------------

  CLOSE( iucy )

  DEALLOCATE( rlon  )
  DEALLOCATE( rlat  )
  DEALLOCATE( wind  )
  DEALLOCATE( vor   )
  DEALLOCATE( psl   )
  if (tracktype .eq. 'gfdl' .OR. tracktype .eq. 'colin' .OR. tracktype .eq. 'winter') then
    DEALLOCATE( tbar  )
    DEALLOCATE( thick )
  elseif (tracktype .eq. 'hodges') then              
    DEALLOCATE( vor200   )
    DEALLOCATE( vor500   )
    DEALLOCATE( vor850_t42   )
  else
    print *,'Unknown tracker selection... exiting...'
  endif

!===================================================================
  end PROGRAM TSTORMS_DRIVE

